package com.leetcode.partition15;

/**
 * @author `RKC`
 * @date 2021/9/8 14:35
 */
public class LC1456定长子串中元音的最大数目 {

    public static int maxVowels(String s, int k) {
        return slidingWindow(s, k);
    }

    public static void main(String[] args) {
        String s = "aeiou";
        int k = 2;
        System.out.println(maxVowels(s, k));
    }

    private static int slidingWindow(String s, int k) {
        int left = 0, right = 0, vowelCount = 0;
        char[] words = s.toCharArray();
        //初始化滑动窗口
        for (int i = 0; i < words.length && right < k; i++) {
            if (isVowel(words[i])) vowelCount++;
            right++;
        }
        int answer = vowelCount;
        for (int i = k; i < words.length && right < words.length; i++, right++, left++) {
            if (isVowel(words[right])) vowelCount++;
            if (isVowel(words[left])) vowelCount--;
            answer = Math.max(answer, vowelCount);
        }
        return answer;
    }

    private static boolean isVowel(char c) {
        if (c == 'a') return true;
        if (c == 'e') return true;
        if (c == 'i') return true;
        if (c == 'o') return true;
        return c == 'u';
    }
}
